package cn.wgx.module.mind.websocket.util;

import cn.wgx.common.util.SpringContextHolder;
import cn.wgx.module.mind.websocket.entity.BaseMessage;
import cn.wgx.module.mind.websocket.entity.WSPrincipal;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;

import java.util.Map;

/**
 *  编剧平台锁定节点工具
 */
public class SWLockUtil {


    private static RedisTemplate redisTemplate = SpringContextHolder.getBean("redisTemplate");

    private static String ROOM_KEY = "SW_LOCK_";

    private static String KEY_ROOM_LIST = "SW_ROOM_LIST";

    private static String KEY_USER_LOCK_LIST = "KEY_USER_LOCK_LIST_";

    private static HashOperations redisSet(){
        return redisTemplate.opsForHash();
    }

    private static HashOperations<String, String, WSPrincipal> redisMap(){
        return redisTemplate.opsForHash();
    }


    //锁定
    public static boolean lock(String project_id, String beat_id, WSPrincipal wsPrincipal){
        if(project_id == null || wsPrincipal == null){
            return false;
        }
        //锁定项目,关联人员
        redisMap().put(ROOM_KEY + project_id, beat_id, wsPrincipal);

        //记录人员锁定的节点
        redisSet().put(KEY_USER_LOCK_LIST + wsPrincipal.getName(), beat_id, project_id);

//        BaseMessage baseMessage = new BaseMessage(wsPrincipal);
//        baseMessage.setMsgType(MessageType.EVENT_SW_LOCK);
//        baseMessage.setContent(beat_id);
//        RoomMsgSender.sendPublicMsg(project_id, baseMessage);
        return true;
    }

    //解锁
    public static boolean unlock(String pro_id, String beat_id,  WSPrincipal wsPrincipal){
        if(pro_id == null || wsPrincipal == null){
            return false;
        }
        redisMap().delete(ROOM_KEY + pro_id, beat_id);
        //删除人员锁定的节点
        redisSet().delete(KEY_USER_LOCK_LIST + wsPrincipal.getName(), beat_id);
//        BaseMessage baseMessage = new BaseMessage(wsPrincipal);
//        baseMessage.setMsgType(MessageType.EVENT_SW_UNLOCK);
//        baseMessage.setContent(beat_id);
//        RoomMsgSender.sendPublicMsg(pro_id, baseMessage);
        return false;
    }

    //退出用户本项目所有锁定
    public static void unlockProByUser(String name, String pro_id) {
        Map entries = redisSet().entries(KEY_USER_LOCK_LIST + name);
        entries.forEach((beat_id, p_id) ->{
            if(pro_id.equals(pro_id)){
                redisMap().delete(ROOM_KEY + pro_id, beat_id);
                //删除人员锁定的节点
                redisSet().delete(KEY_USER_LOCK_LIST + name, beat_id);
            }
        });
    }

    //退出用户锁定所有项目
    public static void unlockAllByUser(String name) {
        Map entries = redisSet().entries(KEY_USER_LOCK_LIST + name);
        entries.forEach((beat_id, p_id) ->{
            redisMap().delete(ROOM_KEY + p_id, beat_id);
            //删除人员锁定的节点
            redisSet().delete(KEY_USER_LOCK_LIST + name, beat_id);

        });
    }

    private static void sendUnlockMsg(String pro_id, String beat_id){
        //{"id":"80fbdb1734e144ed9c8d067792cf7690","content":null,"msgType":"EVENT_SW_UNLOCK","toUser":null,"fromUser":{"name":"UB28D8D66FBB9445EA644960BD7F78915","user_id":1,"username":"admin","nickname":"超级管理员","photo":"http://file.sxycy.cn:8888/group1/M00/00/37/wKgBBV7MhZ2AHr0FAAgBw0K8Pp0954.jpg","type":1,"state":0,"limit":0,"roomId":"194","admin":false,"forbidden":false},"data":{},"roomId":"194","beat_id":862}
        BaseMessage baseMessage = new BaseMessage();
//        baseMessage.setMsgType(MessageType.EVENT_SW_UNLOCK);
//        baseMessage.setContent(beat_id);
//        RoomMsgSender.sendPublicMsg(pro_id, baseMessage);

    }

    //获取某节点锁定用户
    public static WSPrincipal getLockPrincipal(String pro_id, String beat_id){
        if(redisMap().hasKey(ROOM_KEY + pro_id, beat_id)){
            WSPrincipal w = redisMap().get(ROOM_KEY + pro_id, beat_id);
            return w;
        }
        return null;
    }


    //判断项目某节点是否锁定
    public static boolean hasLock(String pro_id, String beat_id){
        return redisMap().hasKey(ROOM_KEY + pro_id, beat_id);
    }

    //判断项目某节点某人是否可以修改
    public static boolean canUpdate(String name, Integer pro_id, Integer beat_id){
        if(name == null || pro_id == null || beat_id == null){
            return false;
        }
        WSPrincipal lockPrincipal = getLockPrincipal(pro_id.toString(), beat_id.toString());
        if(lockPrincipal != null){
            return lockPrincipal.getName().equals(name);
        }
        return false;
    }

    //获取项目内所有锁定节点
    public static Map<String, WSPrincipal> getAllLocks(String pro_id, int start, int limit){
        Map<String, WSPrincipal> entries = redisMap().entries(ROOM_KEY + pro_id);
        return entries;
    }


}
